
# generate figure 3

# load packages
library(rio) # load data
library(tidyverse) # data manipulation
library(scales) # better breaks for plots
library(ggpubr) # combine different plots


# set working directory
setwd("~/replication_files/")

# load data for analysis
dictionary_data <- import("data/full_data.csv") %>%
  filter(article_iv_publication == 1) # to generate these figures, we're only interested in country-years with consultations


# number of consultations that promote natural resource governance
plot1 <- dictionary_data %>% 
  group_by(year) %>%
  summarize(sum_consultations_promoting_governance = sum(article_iv_promotes_governance)) %>%
  ungroup() %>%
  ggplot(aes(x = year, y = sum_consultations_promoting_governance)) + geom_col() + theme_classic() + 
  labs(x = "Year", y = "Number of Consultations",
       title = "(a) Number of Consultations Promoting Natural Resource Governance") +
  theme(plot.title = element_text(face="bold"))  + scale_y_continuous(breaks = breaks_pretty(), limits = c(0,41)) + 
  scale_x_continuous(breaks = c(2004,2009,2014,2019))

# number of consultations that mention natural resources
plot2 <- dictionary_data %>% 
  group_by(year) %>%
  summarize(sum_article_iv_mentions_resources = sum(article_iv_mentions_resources)) %>%
  ungroup() %>%
  ggplot(aes(x = year, y = sum_article_iv_mentions_resources)) + geom_col() + theme_classic() + 
  labs(x = "Year", y = "Number of Consultations",
       title = "(b) Number of Consultations Using Natural Resource Terms") +
  theme(plot.title = element_text(face="bold"))  + scale_y_continuous(breaks = breaks_pretty(), limits = c(0,41)) + 
  scale_x_continuous(breaks = c(2004,2009,2014,2019))

# average number of terms per consultation
plot3 <- dictionary_data %>% 
  group_by(year) %>%
  summarize(mean_mentions_absolute = mean(resource_mentions_absolute)) %>%
  ungroup() %>%
  ggplot(aes(x = year, y = mean_mentions_absolute)) + geom_col() + theme_classic() + 
  labs(x = "Year", y = "Number of Natural Resource Terms",
       title = "(c) Average Number of Natural Resource Terms Per Consultation") +
  theme(plot.title = element_text(face="bold")) + scale_y_continuous(breaks = breaks_pretty(), limits = c(0,5)) + 
  scale_x_continuous(breaks = c(2004,2009,2014,2019))

ggarrange(plot1,plot2) %>%
  ggexport(filename = "figures/fig3_1.pdf", width = 14, height = 5)

plot3 %>%
  ggexport(filename = "figures/fig3_2.pdf", width = 7, height = 5)

